package cn.yikaojiuguo.student.ui.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.support.v4.app.Fragment;

import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by cfan_yang on 2015/12/18.
 */
public class BaseFragment extends Fragment{

    protected String mTag;
    private ProgressDialog mProgressDialog;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mTag = this.getClass().getName();
        mProgressDialog = new ProgressDialog(activity);
        EventBus.getDefault().register(this);
    }

    protected void showProgressDialog(String msg) {
        if(mProgressDialog!=null) {
            mProgressDialog.setMessage(msg);
            mProgressDialog.setCanceledOnTouchOutside(true);
            mProgressDialog.show();
        }
    }

    protected void showProgressDialog() {
        if(mProgressDialog!=null) {
            mProgressDialog.setCanceledOnTouchOutside(true);
            mProgressDialog.show();
        }
    }

    protected void showProgressDialogNotCancel() {
        if(mProgressDialog!=null) {
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();
        }
    }

    protected void showProgressDialogNotCancel(String msg) {
        if(mProgressDialog!=null) {
            mProgressDialog.setMessage(msg);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();
        }
    }

    protected void dismissProgressDialog() {
        if(mProgressDialog!=null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        dismissProgressDialog();
        LogUtil.showInfoLog("Fragment destroy",mTag);
    }

    /**
     * 接收到消息时被调用，有需求的子类重写该方法
     */
    public void onEvent(MessageEvent event){
        if(MessageEvent.MessageType.DISMISS_PROGRESS_DIALOG == event.messageType) {
            dismissProgressDialog();
        }
    }
}
